package com.paomi.client.activity.person;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.person.View.CardView;
import com.paomi.client.activity.person.model.CheckCouponBean;
import com.paomi.client.activity.person.presenter.CardPre;
import com.paomi.client.activity.receive.CardReceiveActivity;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.fragment.card.CardAlreadyUsedFragment;
import com.paomi.client.fragment.card.CardBeOverdueFragment;
import com.paomi.client.fragment.card.CardNotUsedFragment;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyCardActivity extends BaseMvpActivity<CardView, CardPre> implements CardView {
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.button_goback)
    TextView button_goback;
    @BindView(R.id.rb_already_used)
    RadioButton rb_already_used;
    @BindView(R.id.rb_not_used)
    RadioButton rb_not_used;
    @BindView(R.id.rb_be_overdue)
    RadioButton rb_be_overdue;
    @BindView(R.id.tv_exchange)
    TextView tv_exchange;
    @BindView(R.id.et_input)
    EditText et_input;
    @BindView(R.id.btn_receive)
    Button btnReceive;
    @BindView(R.id.btn_exchange)
    Button btnExchange;
    @BindView(R.id.lay_change)
    LinearLayout layChange;

    private CardBeOverdueFragment beOverdueFragment;//已过期
    private CardNotUsedFragment notUsedFragment;//去使用
    private CardAlreadyUsedFragment alreadyUsedFragment;//已使用
    private List<Fragment> fragments;

    @NonNull
    @Override
    public CardPre createPresenter() {
        return new CardPre();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.string_my_card);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_card;
    }

    @Override
    protected void initEvent() {
        button_goback.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));

        beOverdueFragment = new CardBeOverdueFragment();
        notUsedFragment = new CardNotUsedFragment();
        alreadyUsedFragment = new CardAlreadyUsedFragment();
        fragments = new ArrayList<Fragment>();
        fragments.add(notUsedFragment);
        fragments.add(alreadyUsedFragment);
        fragments.add(beOverdueFragment);

        viewpager.setAdapter(new MyFragmentPagerAdapter(MyCardActivity.this.getSupportFragmentManager(), fragments));

        // ViewPager页面切换监听
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        rb_not_used.setChecked(true);
                        break;
                    case 1:
                        rb_already_used.setChecked(true);
                        break;
                    case 2:
                        rb_be_overdue.setChecked(true);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rb_not_used.setOnClickListener(mainListener);
        rb_already_used.setOnClickListener(mainListener);
        rb_be_overdue.setOnClickListener(mainListener);

        et_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!"".equals(s.toString())) {
                    tv_exchange.setClickable(true);//按钮可点击
                    tv_exchange.setBackgroundResource(R.color.color_108EE9);
                } else {
                    tv_exchange.setClickable(false);//按钮不可点击
                    tv_exchange.setBackgroundResource(R.color.color_108EE9_50);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    final View.OnClickListener mainListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rb_not_used:
                    viewpager.setCurrentItem(0);
                    break;
                case R.id.rb_already_used:
                    viewpager.setCurrentItem(1);
                    break;
                case R.id.rb_be_overdue:
                    viewpager.setCurrentItem(2);
                    break;
            }
        }
    };

    @OnClick(R.id.tv_exchange)
    void putSend() {
        if (et_input.getText().toString().trim().isEmpty()) {
            return;
        }

        getCard(et_input.getText().toString().trim());
        //判断优惠券是否可用
//        getPresenter().checkConpon(et_input.getText().toString().trim());
    }

    @Override
    public void getCardSuccess() {
        ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_2view, "");
        et_input.setText("");
        //刷新数据
        notUsedFragment.loadData(true);
    }

    @Override
    public void checkSuccess(CheckCouponBean result) {
        if (result.returnCode.equals("0000")) {
            if (!result.getReturnData()) {
                Util.toast(this, "可领取");
//                getCard();
            } else {
                Util.toast(this, "已领取");
            }
        } else {
            Util.toast(this, result.returnMessage);
        }
    }

    /**
     * 获取优惠券
     * couponId	false	优惠券ID(code 为null时可选 code不为空必选)
     * assType	true	优惠券类型 1.演出 2 衍生品
     * code	false	劵码
     */
    void getCard(String code) {
        Map<String, String> map = new HashMap<>();
        map.put("assType", "1");
        if (code == null || code.equals("")) {
//            map.put("couponId",);
        } else {
            map.put("code", code);
        }
        getPresenter().getCoupon(map);
    }

    @OnClick({R.id.btn_receive, R.id.btn_exchange})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_receive://领取
                startActivityForResult(CardReceiveActivity.class,"",2019);
                break;
            case R.id.btn_exchange://兑换
                getPresenter().showChangeDialog(this);
                break;
        }
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
